package com.huitone.gddw.controller.analysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.CallTestGate;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.service.analysis.ICallTestGateService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 拨测通道 控制器
 *
 */
@Controller
@RequestMapping(value="callTestGate")
public class CallTestGateController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(CallTestGateController.class);
	
	@Autowired
	private ICallTestGateService callTestGateService;
	
	
	/**
	 * 更改状态
	 * @param recId 记录ID
	 * @param status 更改后状态
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeStatus")
	public Message changeStatus(String recId, String status) {
		try {
			callTestGateService.changeStatus(recId,status);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改拨测通道状态过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param recId 记录ID
	 * @return BlackUseDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public CallTestGate toUpdate(String recId) {
		try {
			CallTestGate entity = callTestGateService.toUpdate(recId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new CallTestGate();
		}
	}
	
	/**
	 * 保存
	 * @param entity CallTestGate
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(CallTestGate entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加拨测通道";
		boolean isSuccess = true;
		String msg;
		try {
			List<CallTestGate> list = callTestGateService.selectByGateLinkId(entity.getGateLinkId());
			if (list.size() > 0) {
				isSuccess = false;
				msg = "该拨测通道已存在！";
			} else {
				callTestGateService.save(entity);
				msg = "保存成功！";
			}
		} catch (Exception e) {
			LOGGER.error("保存拨测通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 更新
	 * @param entity CallTestGate
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(CallTestGate entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改拨测通道";
		boolean isSuccess = true;
		String msg;
		try {
			CallTestGate callTestGate = callTestGateService.selectByPrimaryKey(entity.getRecId());
			callTestGate.setStatus(entity.getStatus());
			callTestGateService.update(callTestGate);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改拨测通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param recId 记录ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除拨测通道";
		boolean isSuccess = true;
		String msg;
		try {
			callTestGateService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除拨测通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="getTestGate")
	public List<CallTestGate> getCallTest(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		List<CallTestGate> list = callTestGateService.getRecord(params);
		return list;
	}
	
}
